package com.sh.cloudrender.infrastructure.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DataSourceConfiguration {
    @Value("${datasource.driver-class-name}")
    private String dbDriver;
    @Value("${datasource.url}")
    private String dbUrl;
    @Value("${datasource.username}")
    private String dbUser;
    @Value("${datasource.password}")
    private String dbPassword;


    public DataSourceConfiguration() {
    }

    @Bean(name = {"mysqlDataSource"})
    @Primary
    public DataSource rdsDataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.dbDriver);
        dataSource.setUrl(this.dbUrl);
        dataSource.setUsername(this.dbUser);
        dataSource.setPassword( this.dbPassword);
        dataSource.setLoginTimeout(3000);
        dataSource.setInitialSize(5);
        dataSource.setMaxActive(50);
        dataSource.setMaxWait(2000L);
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery("select 1");
        dataSource.setTimeBetweenEvictionRunsMillis(150000L);
        dataSource.setMinEvictableIdleTimeMillis(300000L);
        dataSource.setConnectionProperties("connectTimeout=1000;socketTimeout=12000");
        dataSource.init();
        return dataSource;
    }
}
